package com.huajx.ld.codec;

import java.io.Closeable;
import java.nio.charset.Charset;

import com.huajx.util.StrUtil;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TbEncoderAdapter implements ProtocolEncoder {
	private final static Logger logger = LoggerFactory.getLogger(TbEncoderAdapter.class);
	
	private static final AttributeKey KEY_ENCRYPTOR = new AttributeKey(TbEncoderAdapter.class, "Encryptor");
	private static final AttributeKey KEY_SECRETKEY = new AttributeKey(TbEncoderAdapter.class, "SecretKey");
	private final Charset codePage;
	private final boolean isServer;
	private final boolean isPassiveEncryption;

	public TbEncoderAdapter(Charset charset, boolean isServer, boolean isPassiveEncryption) {
		this.codePage = charset;
		this.isServer = isServer;
		this.isPassiveEncryption = isPassiveEncryption;
	}

	/**
	 * 编码
	 */
	@Override
	public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
		if(message == null){
			return;
		}
		
		if(message instanceof byte[]){
			out.write(IoBuffer.wrap((byte[])message));
			return;
		}else if(message instanceof String){
			String msg = (String)message;
			byte[] source = msg.getBytes();
			out.write(IoBuffer.wrap(source));
			return;
		}
		
		String msg = message.toString();
		if (StrUtil.isBlank(msg)) {
			return;
		}
		
//		if(message == null){
//			return;
//		}
		byte[] source = msg.getBytes();//msg.getBytes(this.codePage);
//		out.write(message);
		out.write(IoBuffer.wrap(source));
	}

	@Override
	public void dispose(IoSession session) throws Exception {
		Closeable Encryptor = (Closeable) session.getAttribute(KEY_ENCRYPTOR);
		if (Encryptor != null) {
			Encryptor.close();
		}
		session.removeAttribute(KEY_ENCRYPTOR);
		session.removeAttribute(KEY_SECRETKEY);
	}

//	protected Object getEncryptor(IoSession session, String secretKey) throws IOException {
//		Object Encryptor = session.getAttribute(KEY_ENCRYPTOR);
//		if (Encryptor == null) {
//			if (StrKit.notBlank(secretKey)) {
//				if (this.isServer) {
//					// Encryptor = new SM4(CipherDirection.Encryption,
//					// CipherMode.OFB, PaddingMode.PKCS7);
//					//
//					// byte[] DerivedVector =
//					// SM3.KDF(secretKey.getBytes("UTF-8"), 32);
//					// ((SM4)Encryptor).Initialize(Arrays.copyOfRange(DerivedVector,
//					// 0, 16), Arrays.copyOfRange(DerivedVector, 16, 32));
//				} else {
//					// Encryptor = new Xor64Codec(secretKey);
//				}
//				session.setAttribute(KEY_ENCRYPTOR, Encryptor);
//			}
//		}
//		return Encryptor;
//	}

	public Charset getCharset() {
		return this.codePage;
	}

	public boolean getIsServer() {
		return this.isServer;
	}

	public boolean getIsPassiveEncryption() {
		return this.isPassiveEncryption;
	}

	public static void setSecretKey(IoSession session, String key) {
		session.setAttribute(KEY_SECRETKEY, key);
	}

	public static String getSecretKey(IoSession session) {
		return (String) session.getAttribute(KEY_SECRETKEY);
	}
}
